#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Módulos 
import sys, gtk, pygame, utils, objetos, inputbox
from pygame.locals import *
from string import letters
from string import whitespace

# Constantes

# Clases
# ---------------------------------------------------------------------
class Personajes(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = utils.load_image("images/personajes1.png", True)
        self.rect = self.image.get_rect()
        self.rect.centerx = 150*utils.RES
        self.rect.centery = 260*utils.RES

class Textbox:
	def __init__(self,posx,posy):
		self.font = pygame.font.Font("fonts/TRSL____.ttf", int(18*utils.RES))
		self.rect = pygame.Rect([0, 0, 220*utils.RES, 22*utils.RES])
		offset = (3, 3)
		self.rect.centerx = posx
		self.rect.centery = posy
		self.rect.left += offset[0]
		self.rect.top  += offset[1]
		self.message = ""
		self.selected = False
		self.puntos,self.puntos_rect = utils.texto_normal('..............................', posx-20*utils.RES,posy+10*utils.RES)
		self.maxlen = 20
		self.sonido = pygame.mixer.Sound("sounds/letra.ogg")
		
	def display(self,screen):
		screen.blit(self.puntos,self.puntos_rect)
		if len(self.message) != 0:
			#self.message = self.message.encode("iso-8859-1")
			screen.blit(self.font.render(self.message, 1, (0,0,0)), self.rect.topleft)	
   

# ---------------------------------------------------------------------
def dibujar(screen,cursor_picture, clock):
    salir = 0
    background_image = utils.load_image('images/fondo1_2.png')
    i= 0
    j= 0
    numero_linea = 1
    btn_adelante = objetos.BotonAdelante()
    btn_atras = objetos.BotonAtras()
    clase1, clase1_rect = utils.texto_clase("Clase 1", 750*utils.RES, 15*utils.RES)
           
    #titulo, titulo_rect = utils.texto_subtitulo("Que es el medio ambiente?", 380*utils.RES, 80*utils.RES)
    linea1,linea1_rect = utils.texto_normal('Completa esta tabla con otros componentes del ambiente que hayas', 345*utils.RES,65*utils.RES)
    linea2,linea2_rect = utils.texto_normal('encontrado en las ilustraciones:', 200*utils.RES,90*utils.RES)
    linea3,linea3_rect = utils.texto_normal('Medio ambiente urbano', 380*utils.RES,170*utils.RES)
    linea4,linea4_rect = utils.texto_normal('Medio ambiente rural', 620*utils.RES,170*utils.RES)
    linea5,linea5_rect = utils.texto_normal('Vivos', 135*utils.RES,270*utils.RES)
    linea6,linea6_rect = utils.texto_normal('No vivos', 145*utils.RES,390*utils.RES)
    linea7,linea7_rect = utils.texto_normal('Elementos', 150*utils.RES,170*utils.RES)
    
    cuadro = utils.load_image('images/c1_cuadro.png', False)
    s = ""
    textboxes = (Textbox(400*utils.RES,270*utils.RES),Textbox(400*utils.RES,390*utils.RES),Textbox(640*utils.RES,270*utils.RES),Textbox(640*utils.RES,390*utils.RES))
    frame = 0
    
    while salir == 0:
    
    	# Pump GTK messages.
		while gtk.events_pending():
			gtk.main_iteration()
			
		for eventos in pygame.event.get():
			if eventos.type == QUIT:
				sys.exit(0)
			elif eventos.type == pygame.VIDEORESIZE:
			   pygame.display.set_mode(eventos.size, pygame.RESIZABLE)	
			elif eventos.type == MOUSEBUTTONDOWN:
				if btn_adelante.rect.collidepoint(pygame.mouse.get_pos()):
					salir = 6
					btn_adelante.sonido.play()	
				elif btn_atras.rect.collidepoint(pygame.mouse.get_pos()):
					salir = 4
					btn_atras.sonido.play()	
				for textbox in textboxes:
					if textbox.rect.collidepoint(pygame.mouse.get_pos()):
						textbox.selected = True
						textbox.sonido.play()
					else:
						textbox.selected = False
			
			elif eventos.type == KEYDOWN:		
				if eventos.key == K_BACKSPACE:
					for textbox in textboxes:
						if textbox.selected == True: 
							textbox.message = textbox.message[0:-1]
				elif eventos.key == K_RETURN:
					break
				else:
					for textbox in textboxes:
						if textbox.selected == True:
							textbox.sonido.play() 
							if  len(textbox.message) <= textbox.maxlen:
								textbox.message += eventos.unicode.encode("iso-8859-1")		
					
		# Realiza la animacion del texto
				
		screen.blit(background_image, (0, 0))
		screen.blit(clase1, clase1_rect)
		if 20	< frame:
			screen.blit(linea1,linea1_rect.topleft)
			screen.blit(linea2,linea2_rect.topleft)
		if 50	< frame:
			screen.blit(cuadro, (70*utils.RES,120*utils.RES))
			screen.blit(linea3,linea3_rect)
			screen.blit(linea4,linea4_rect)
			screen.blit(linea5,linea5_rect)
			screen.blit(linea6,linea6_rect)
			screen.blit(linea7,linea7_rect)
			for textbox in textboxes:
				textbox.display(screen)
		screen.blit(btn_adelante.image, btn_adelante.rect)
		screen.blit(btn_atras.image, btn_atras.rect)	
		screen.blit(cursor_picture, pygame.mouse.get_pos())
		pygame.display.flip()
		
		# Try to stay at 30 FPS
		clock.tick(30)
		frame = frame + 1
                	 
    return salir

# This function is called when the game is run directly from the command line:
# ./TestGame.py 
def main():
	
	pygame.init()
	screen = pygame.display.set_mode((int(800*utils.RES), int(520*utils.RES)), pygame.RESIZABLE) #pygame.display.get_surface() #

	# Fake a Sugar cursor for the pyGame canvas area
	pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0))
	cursor_picture = pygame.image.load('images/standardcursor.png')
	cursor_picture.convert_alpha() 
	clock = pygame.time.Clock()
	dibujar(screen,cursor_picture,clock)

if __name__ == '__main__':
   main()
